<template>
  <div id="parm">
    <el-form ref="form" :rules="rules" :model="form" label-width="80px" >
      <el-form-item prop="PUSH_MSG_TIME" label="推送时间">
          <el-time-picker
            v-model="form.PUSH_MSG_TIME"
            :picker-options="{
            selectableRange: '00:00:00 - 23:59:59'
            }" 
            format='HH:mm'
            placeholder="任意时间点" :disabled="status==1">
          </el-time-picker> 
      </el-form-item>
      <el-form-item prop="PUSH_MSG_CONTENT" label="推送内容">
        <el-input type="textarea" :autosize="{ minRows: 2, maxRows: 4}" v-model.trim="form.PUSH_MSG_CONTENT" placeholder="请输入内容" :disabled="status==1"></el-input>  
      </el-form-item>
      <el-form-item prop="SEND_CARD_TIME" label="派发时间">
        <el-time-picker
            v-model="form.SEND_CARD_TIME"
            :picker-options="{
            selectableRange: '00:00:00 - 23:59:59'
            }"
            format='HH:mm'
            placeholder="任意时间点" :disabled="status==1">
          </el-time-picker> 
      </el-form-item>
    </el-form>
    <stepBtn :nextBtn="next" :successBtn="success" :prevBtn="prev" :saveBtn="save" :btnShow="btnShow" ></stepBtn>
  </div>
</template>
<script>
import moment from 'moment'
import stepBtn from '../stepButton'
import {formRules} from './config'
import {activityParm, getActParam} from 'api/activity'
export default {
  components: {
    stepBtn
  },
  props: ['edit'],
  data () {
    return {
      btnShow: {                                                                                         //控制按钮展示
        next: false,
        prev: true,
        save: false,
        success: false
      },
      prevTap: '',
      nextTap: '',
      form: {
        PUSH_MSG_TIME: '',
        PUSH_MSG_CONTENT: '',
        SEND_CARD_TIME: ''
      },
      PUSH_MSG_TIME: '',
      PUSH_MSG_CONTENT: '',
      SEND_CARD_TIME: '',
      rules: formRules
    }
  },
  computed: {
    actTypeInfo () {
      return JSON.parse(sessionStorage.getItem('addActivityType'))
    },
    activityId () {
      return JSON.parse(sessionStorage.getItem('activityId'))
    },
    status () {
      return sessionStorage.getItem('actStatus')
    }
  },
  created () {
    if (!this.edit) {
      this.checkStep();
    } else {
      this.btnShow.save = true
      this.btnShow.prev = false
    }
    this.getParm()
  },
  methods: {
    async getParm () {
      try {
        let back = await getActParam(this.activityId)
        back = back.data
        if (back.resultCode === '200') {
          if (back.result.length !== 0) {
            let arr = [...back.result]
            arr.forEach(element => {
              if (element.key === 'PUSH_MSG_TIME' || element.key === 'SEND_CARD_TIME') {
                element.value = '2018-01-01 ' + element.value + ':00'
              }
              this[element.key] = element.id
              this.form[element.key] = element.value
            });
          }
        }
      } catch (err) {
        this.$alert('请求出错' + err, '提示')
      }
    },
    checkStep () {
      let stepArr = this.actTypeInfo.relatedSectionsArray;
      //现在处于第几步
      this.nowIndex = stepArr.indexOf('PARAM');
      //下一步
      let nextIndex = this.nowIndex + 1;
      let prevIndex = this.nowIndex - 1;
      // 如果这是最后一步了
      if (nextIndex === stepArr.length) {
        this.btnShow.success = true;
        this.prevTap = stepArr[prevIndex];
      } else {
        this.btnShow.next = true;
        this.nextTap = stepArr[nextIndex];
        this.prevTap = stepArr[prevIndex];
      }
    },
    prev () {
      this.$emit('changePageShow', this.prevTap, 'prev')
    },
    next () {
      this.submit('next')
    },
    success () {
      this.submit('success')
    },
    save () {
      this.submit('save')
    },
    async submit (type) {
      if (this.form.PUSH_MSG_TIME === '' || this.form.SEND_CARD_TIME === '') {
        this.$alert('请选择时间', '提示')
      }
      this.$refs['form'].validate((valid) => {
        if (valid) {
          let arr = [
            {
              'activityId': this.activityId,
              'key': 'PUSH_MSG_TIME',
              'value': moment(this.form.PUSH_MSG_TIME).format('HH:mm'),
              'id': this.PUSH_MSG_TIME
            },
            {
              'activityId': this.activityId,
              'key': 'PUSH_MSG_CONTENT',
              'value': this.form.PUSH_MSG_CONTENT,
              'id': this.PUSH_MSG_CONTENT
            },
            {
              'activityId': this.activityId,
              'key': 'SEND_CARD_TIME',
              'value': moment(this.form.SEND_CARD_TIME).format('HH:mm'),
              'id': this.SEND_CARD_TIME
            }
          ];
          activityParm(arr)
            .then((res) => {
              let back = res.data
              if (back.resultCode === '200') {
                if (type === 'next') {
                  this.$emit('changePageShow', this.nextTap)
                } else if (type === 'success') {
                  this.$router.push({name: 'activityList'})
                } else if (type === 'save') {
                  this.$alert('保存成功', '提示')
                }
              } else {
                this.$alert(back.result, back.msg)
              }
            }, (rej) => {
              this.$alert('请求出错', '提示')
            })
          return true
        } else {
          return false
        }
      })
    }
  }
}
</script>
<style lang="less" scoped>

</style>
